const axios = require("axios");
const fs = require("fs");
const cheerio = require("cheerio");
const { Cheerio } = require("_cheerio@1.0.0-rc.10@cheerio/lib/cheerio");

async function indexData() {
    let options = {
        url: "https://www.leishen.cn/",
        headers: {
            "User-Agent":
                `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36`,
            "cookie": `PHPSESSID=kkonri78vl9rhbk74ehbkigtoo; stat_nuvtime=1630575536676; stat_uvtime=1630662012359; index_top_adv=1; Hm_lvt_fbd7f5aff59c87a3fde2ddb996e93517=1630575537,1630662008,1630670770,1630671728; Hm_lvt_1463e5964a3a2e562960ae5acd14d4e9=1630575537,1630662008,1630670770,1630671728; goodshistory=1115%2C1085; Hm_lpvt_fbd7f5aff59c87a3fde2ddb996e93517=1630672008; Hm_lpvt_1463e5964a3a2e562960ae5acd14d4e9=1630672008`
        }
    }
    let res = await axios(options);
    let $ = cheerio.load(res.data);

    let good_pic = $(".goods-pic img");
    let good_title = $(".googs-title");
    let good_price = $(".goods-price");

    let thumb_pic = $(".rainbow-item-1 .thumb img");
    let thumb_title = $(".rainbow-item-1 .title a");
    let thumb_desc = $(".rainbow-item-1 .desc");

    let incre_pic = $(".new_add .box-bd .xm-carousel-wrapper .thumb img");
    let incre_title = $(".new_add .box-bd .title");
    let incre_price = $(".new_add .box-bd .price");

    let hot_pic = $("#hot-comment .figure img");
    let hot_review = $("#hot-comment .review");
    let hot_title = $("#hot-comment .title a");
    let hot_price = $("#hot-comment .price");

    let city_pic = $("#same_city img");
    let city_title = $("#same_city .title a");
    let city_a = $("#same_city .figure a");
    let hotel_pic = $("#same_ehotel img");
    let hotel_title = $("#same_ehotel .title a");
    let hotel_a = $("#same_ehotel .figure a");

    let brand_small = $(".brand-samll img")
    let brand_sa = $(".brand-samll")
    let brand_big = $(".brand-big img")
    let brand_ba = $(".brand-big")
    

    let floor_pic = $(".floor-recommend-pic img");
    let floor_a = $(".floor-recommend-item[href]");
    let floor_title = $(".floor-recommend-cont .recommend-goods-name");
    let floor_price = $(".floor-recommend-cont .recommend-goods-price");

    let goods = [];
    let thumb = [];
    let increment = [];
    let hot = [];
    let city = [];
    let hotel = [];
    let brand = [];
    let floorsw = [];

    for (let i = 0; i < good_pic.length; i++) {
        goods.push({
            good_pic: good_pic.eq(i).attr("src"),
            good_title: good_title.eq(i).text(),
            good_price: good_price.eq(i).text(),
        })
    }

    for (let j = 0; j < thumb_pic.length; j++) {
        thumb.push({
            thumb_pic: thumb_pic.eq(j).attr("src"),
            thumb_title: thumb_title.eq(j).text(),
            thumb_desc: thumb_desc.eq(j).text(),
        })
    }

    for (let k = 0; k < incre_pic.length; k++) {
        increment.push({
            incre_pic: incre_pic.eq(k).attr("src"),
            incre_title: incre_title.eq(k).text(),
            incre_price: incre_price.eq(k).text(),
        })
    }

    for (let x = 0; x < hot_pic.length; x++) {
        hot.push({
            hot_pic: hot_pic.eq(x).attr("src"),
            hot_preve : hot_review.eq(x).text(),
            hot_title: hot_title.eq(x).text(),
            hot_price: hot_price.eq(x).text(),
        })
    }

    for(var y = 0; y < city_pic.length; y++){
        city.push({
            city_pic: city_pic.eq(y).attr("src"),
            city_title: city_title.eq(y).text(),
            city_a : city_a.eq(y).attr("href")
        })
    }
    for(y = 0; y < hotel_pic.length; y++){
        hotel.push({
            hotel_pic: hotel_pic.eq(y).attr("src"),
            hotel_title: hotel_title.eq(y).text(),
            hotel_a : hotel_a.eq(y).attr("href")
        })
    }

    for(let z = 0; z < brand_big.length; z++){
        brand.push({

            brand_small: brand_small.eq(z).attr("src"),
            brand_sa : brand_sa.eq(z).attr("href"),
            brand_big: brand_big.eq(z).attr("src"),
            brand_ba : brand_ba.eq(z).attr("href"),
        })
    }

    for(let m = 0; m < floor_pic.length; m++){
        floorsw.push({
            floor_pic : floor_pic.eq(m).attr("src"),
            floor_title : floor_title.eq(m).text(),
            floor_price : floor_price.eq(m).text(),
            floor_a : floor_a.eq(m).attr("href"),
        })
    }
    

    arr = {
        "goods": goods,
        "thumb": thumb,
        "increment" : increment,
        "hot" : hot,
        "city" : city,
        "hotel" : hotel,
        "brand" : brand,
        "floorsw" : floorsw,
    }

    fs.writeFile("../data/index_goods.json", JSON.stringify(arr), err => {
        if (err) throw err;
        console.log(brand_small.length);
    })
}

// indexData();

async function active(){
    let options = {
        url: "https://www.leishen.cn/oneselfDetail/18?order_from_str=MDAwMDAwMDAwMJdogc-Mia7IkKB9qJiHn9mSZIZqmnyGb4S4jKnHorPNmKFw0ZWendaOZGSokHm82IqIbamSeWGuhLmVaA",
        headers: {
            "User-Agent":
                `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36`,
            "cookie": `PHPSESSID=kkonri78vl9rhbk74ehbkigtoo; stat_nuvtime=1630575536676; stat_uvtime=1630662012359; index_top_adv=1; Hm_lvt_fbd7f5aff59c87a3fde2ddb996e93517=1630575537,1630662008,1630670770,1630671728; Hm_lvt_1463e5964a3a2e562960ae5acd14d4e9=1630575537,1630662008,1630670770,1630671728; goodshistory=1115%2C1085; Hm_lpvt_fbd7f5aff59c87a3fde2ddb996e93517=1630672008; Hm_lpvt_1463e5964a3a2e562960ae5acd14d4e9=1630672008`
        }
    }
    let res = await axios(options);
    fs.writeFile("../data/active.html", res.data, err => {
        if (err) throw err;
        console.log("over");
  })
}

// active()


async function listData(){
    let options = {
        url: "https://www.leishen.cn/list/2/4",
        headers: {
            "User-Agent":
                `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36`,
            "cookie": `PHPSESSID=kkonri78vl9rhbk74ehbkigtoo; stat_nuvtime=1630575536676; stat_uvtime=1630662012359; index_top_adv=1; Hm_lvt_fbd7f5aff59c87a3fde2ddb996e93517=1630575537,1630662008,1630670770,1630671728; Hm_lvt_1463e5964a3a2e562960ae5acd14d4e9=1630575537,1630662008,1630670770,1630671728; goodshistory=1115%2C1085; Hm_lpvt_fbd7f5aff59c87a3fde2ddb996e93517=1630672008; Hm_lpvt_1463e5964a3a2e562960ae5acd14d4e9=1630672008`
        }
    }
    let res = await axios(options);
    let $ = cheerio.load(res.data);
    let arr = [];

    // let mid_pic = $(".ps-item");
    // let small_pic = $(".ps-item img")
    let duct_id = $(".p-img a");
    let price = $(".product-list .p-price i");
    let name = $(".product-list .p-name a");
    // let icons = $(".product-list .icons");

    for(var i = 0 ; i < duct_id.length; i++){
        let mid_pic = $(`.clearfix>li[data-index=${i}] .ps-item`);
        let small_pic = $(`.clearfix>li[data-index=${i}] .ps-item>img`);
        let items = [];
        let hot = null;
        let ne = null;

        if($(`.clearfix>li[data-index=${i}] .icons-hot`).length !== 0)
            hot = "热卖";
        if($(`.clearfix>li[data-index=${i}] .icons-new`).length !== 0)
            ne = "新品";

        for(var j = 0; j < mid_pic.length; j++){
            items.push({
                big : mid_pic.eq(j).attr("data-max-src"),
                samll : small_pic.eq(j).attr("src")
            })
        }
        arr.push({
            duct_id : duct_id.eq(i).attr("href").match(/\d+(?=\.html)/g)[0],
            price : price.eq(i).text(),
            name : name.eq(i).text(),
            pics : items,
            hot : hot,
            ne : ne,
        })
    }

    fs.writeFile("../data/list_goods(4).json", JSON.stringify(arr), err => {
        if (err) throw err;
        console.log(duct_id.length);
    })

}

// listData();

var arr = [];
async function details(id){
    let options = {
        url: `https://www.leishen.cn/item/${id}.html`,
        headers: {
            "User-Agent":
            `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36`,
            "cookie": `PHPSESSID=kkonri78vl9rhbk74ehbkigtoo; stat_nuvtime=1630575536676; stat_uvtime=1630662012359; index_top_adv=1; Hm_lvt_fbd7f5aff59c87a3fde2ddb996e93517=1630575537,1630662008,1630670770,1630671728; Hm_lvt_1463e5964a3a2e562960ae5acd14d4e9=1630575537,1630662008,1630670770,1630671728; goodshistory=1115%2C1085; Hm_lpvt_fbd7f5aff59c87a3fde2ddb996e93517=1630672008; Hm_lpvt_1463e5964a3a2e562960ae5acd14d4e9=1630672008`
        }
    }
    let res = await axios(options);
    let $ = cheerio.load(res.data);
    let name = $(".goods-detail .basic-info-wrap h1")
    let info = $(".goods-detail .basic-info-wrap .desc")
    let price = $("#xsgood_price");
    let pics = $("#tab_detail>p img");
    let mid_pic = $(`.goods-detail .preview-wrap #magnifier-wrap .spec-items li img`);
   
    let depicarr = [];
    let picsarr = []

    for(var j = 0; j < mid_pic.length; j++){
        picsarr.push({
            big : mid_pic.eq(j).attr("data-maxsrc"),
            mid : mid_pic.eq(j).attr("data-lsrc"),
            samll : mid_pic.eq(j).attr("src")
        })
    }

    for(let i = 0; i < pics.length; i++){
        depicarr.push({img : pics.eq(i).attr("src")})
    };

    arr.push({
        id : id,
        name : name.text(),
        desc : info.text(),
        pics : picsarr,
        price : price.text(),
        depics : depicarr,
    })
    
    fs.writeFile("../data/details.json", JSON.stringify(arr), err => {
        if (err) throw err;
        console.log("over");
    })
}
// details("779")
// details("777")
// details("766")
// details("737")
// details("729")
// details("638")
// details("637")
// details("636")
// details("635")
// details("634")
// details("633")
// details("627")
// details("611")
// details("599")
// details("552")
// details("538")
// details("519")
// details("411")
// details("392")
// details("343")
// details("328")
// details("248")
// details("71")
// details("55")
// details("53")
// details("1147")
// details("1145")
// details("1144")
// details("1142")
// details("1141")
// details("1140")
// details("1139")
// details("1138")
// details("1137")
// details("1135")
// details("1134")
// details("1133")
// details("1132")
// details("1130")
// details("1128")
// details("1126")
// details("1123")
// details("1122")
// details("1121")
// details("1120")
// details("1118")
// details("1115")
// details("1112")
// details("1110")
// details("1109")
// details("1108")
// details("1104")
// details("1103")
// details("1102")
// details("1101")
// details("1100")
// details("1099")
// details("1098")
// details("1097")
// details("1095")
// details("1094")
// details("1090")
// details("1089")
// details("1086")
